# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Auto_api_model
# FileName:      request_method.py
# Author:       xgy
# Datetime:     2025/9/13 14:18
# Description:
# ---------------------------------------------------------------------------
import requests.sessions

from ApiAutoTest.reader_utils.read_ini import Ini


class RequestMethod:
    def __init__(self):
        # 前置条件 获取登录状态
        # 创建Session 对象
        self.bpm_sess = requests.sessions.Session()

        login_url = Ini().get_host("bpm")+"/auth"
        login_data = {"username":"admin","password":"AZZd8K3LPL7GC/yGL/UAr7+/vTva4D4rHiWpizwOseCJBo4W7VzdfEVawK6XCwV5ZDfN4Xvp2KXguEwNoicdAgrIcesBTRaF/aPi+e6Czn8SXg2LjI0Tn2JVGyP6DdDINusTAEtac5RmGcJlBRa+zX4/xVWNkzvMQZxovdkuIxY="}

        res = self.bpm_sess.request(method="post",url=login_url,json=login_data)
        token = res.json().get("token")

        self.bpm_sess.headers["Authorization"] = "Bearer "+token

    def req(self,req_method,req_url,req_mine,data):
        """
        :param req_method:   公共的请求方法
        :param req_url: 请求路径
        :param req_mine: 媒体类型
        :param data:  测试数据
        :return: Response 类型
        """

        # 判断媒体类型为json或者为application/json 并且判断如果data为字符串的时候，参数应该使用data ,并且配置请求头
        if req_mine == "json" or req_mine == "application/json":
            if isinstance(data,str):
                return self.bpm_sess.request(method=req_method,url=req_url,data=data,headers={"content_type":"application/json"})
            else:
                return self.bpm_sess.request(method=req_method,url=req_url,json=data)
        # 表单类型使用data参数传参，并且数据类型为字符串的时候需要自己配置出一个表单类型的请求头
        elif req_mine =="form" or req_mine == "x-www-form-urlencoded" or req_mine =="application/x-www-form-urlencoded":
            return self.bpm_sess.request(method=req_method,url=req_url,data=data,headers={"content_type":"application/x-www-form-urlencoded"})
        # 地址栏传参
        elif req_mine == "query":
            return self.bpm_sess.request(method=req_method,url=req_url,params=data)
        # 无需要传参的类型
        elif req_mine is None:
            return  self.bpm_sess.request(method=req_method,url=req_url)
        else:
            raise ValueError("媒体类型错误")



if __name__ == '__main__':
    req = RequestMethod()






